package com.huluobo.mvvmdemo20032.repositionies;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvmdemo20032.model.CartItem;
import com.huluobo.mvvmdemo20032.model.Product;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lc on 2022/9/28.
 */
public class CartRepo {
    private MutableLiveData<List<CartItem>> mutableCart = new MutableLiveData<>();

    public LiveData<List<CartItem>> getCart() {
        if (mutableCart.getValue() == null) {
            initCart();
        }
        return mutableCart;
    }

    private void initCart() {
        mutableCart.setValue(new ArrayList<CartItem>());
    }

    public boolean addItemToCart(Product product) {
        if (mutableCart.getValue() == null) {
            initCart();
        }

        List<CartItem> cartItemList = new ArrayList<>(mutableCart.getValue());

        //购物车去重,+1
        for (CartItem cartItem : cartItemList) {
            if (cartItem.getProduct().getId().equals(product.getId())) {
                if (cartItem.getQuantity() == 5) {
                    return false;
                }

                int index = cartItemList.indexOf(cartItem);
                cartItem.setQuantity(cartItem.getQuantity() + 1);
                cartItemList.set(index, cartItem);

                mutableCart.setValue(cartItemList);
                return true;
            }
        }

        CartItem cartItem = new CartItem(product, 1);
        cartItemList.add(cartItem);

        mutableCart.setValue(cartItemList);
        return true;

    }

    public void removeItemFromCart(CartItem cartItem) {
        if (mutableCart.getValue() != null) {
            List<CartItem> cartItemList = new ArrayList<>(mutableCart.getValue());

            cartItemList.remove(cartItem);
            mutableCart.setValue(cartItemList);
        }
    }
}
